package com.smalls.redshoes.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.internal.view.SupportMenu;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.base.BaseFragment;
import com.smalls.redshoes.R;
import com.smalls.redshoes.ui.activity.LivePlayActivity;
import com.smalls.redshoes.ui.activity.RSVodActivity;

public class RSHomeFragment extends BaseFragment implements OnClickListener, OnFocusChangeListener {
    private FrameLayout fl_live;
    private FrameLayout fl_vod;
    private TextView tv_remaining_day;

    private void initHomeData() {
    }

    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.rs_fragment_rshome);
        this.tv_remaining_day = (TextView) getViewById(R.id.tv_remaining_day);
        this.fl_live = (FrameLayout) getViewById(R.id.fl_live);
        this.fl_vod = (FrameLayout) getViewById(R.id.fl_vod);
        this.fl_live.setOnClickListener(this);
        this.fl_vod.setOnClickListener(this);
        this.fl_live.setOnFocusChangeListener(this);
        this.fl_vod.setOnFocusChangeListener(this);
        this.fl_live.requestFocus();
    }

    protected void setListener() {
    }

    protected void processLogic(Bundle savedInstanceState) {
        initHomeData();
    }

    public SpannableStringBuilder setTextType(long value) {
        String text = String.format("\u60a8\u7684\u6709\u6548\u89c2\u770b\u5269\u4f59\u65f6\u95f4: %1$d \u5929", new Object[]{Long.valueOf(value)});
        int start = text.indexOf(":");
        int end = text.indexOf("\u5929");
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.setSpan(new RelativeSizeSpan(1.5f), start + 1, end, 33);
        style.setSpan(new ForegroundColorSpan(SupportMenu.CATEGORY_MASK), start + 1, end, 34);
        return style;
    }

    public void onClick(View v) {
        int i = v.getId();
        Intent intent;
        if (i == R.id.fl_live) {
            intent = new Intent(this.mActivity, LivePlayActivity.class);
            startActivity(intent);

        } else {
            intent = new Intent(this.mActivity, RSVodActivity.class);
            startActivity(intent);
        }
    }

    public void onFocusChange(View view, boolean hasFocus) {
        focusChange(view, hasFocus);
    }

    private void focusChange(View view, boolean hasFocus) {
        Animation mAnimFocus = AnimationUtils.loadAnimation(NewDTVApp.getApp(), R.anim.scale);
        Animation mAnimUnFocus = AnimationUtils.loadAnimation(NewDTVApp.getApp(), R.anim.scaleback);
        mAnimFocus.setFillAfter(true);
        mAnimUnFocus.setFillAfter(true);
        if (hasFocus) {
            view.bringToFront();
            view.startAnimation(mAnimFocus);
            return;
        }
        view.startAnimation(mAnimUnFocus);
        view.bringToFront();
    }
}
